package com.stylefeng.guns.rest.modular.viewrecord.controller;

import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.common.persistence.model.Article;
import com.stylefeng.guns.rest.modular.artitle.service.ArticleService;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.RecordListRequest;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.ShareRecordRequest;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.ViewRecordRequest;
import com.stylefeng.guns.rest.modular.viewrecord.service.ShareRecordService;
import com.stylefeng.guns.rest.modular.viewrecord.service.ViewRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by ASUS-PC on 2018/6/28.
 */
@RestController
@RequestMapping("/shareRecord")
public class ShareRecordController {

    @Autowired
    ShareRecordService shareRecordService;
    @Autowired
    ArticleService articleService;

    //插入分享记录
    @RequestMapping(value = "/addShareRecord", method = RequestMethod.POST)
    public ResponseEntity<?> viewRecordList(@RequestBody ShareRecordRequest request) {
        if(request.getArtitleId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        int state = shareRecordService.insertRecord(request);
        //更新文章的分享记录
        Article article = articleService.queryArtitle(request.getArtitleId());
        if(article!=null){
            article.setShareNum(article.getShareNum()+1);
            articleService.uqdateShareNum(article);
        }
        return ResponseEntity.ok(CommonResponse.ok(null));

    }

}
